<template>
  <div class="add">
    <el-button type="primary" plain v-on:click="returnMain()">返回</el-button>
    <h1 align="center">发布新闻</h1>
    <div style="margin: 50px 0;"></div>
    <span>新闻标题:</span>
    <el-input v-model="news.title" placeholder="请输入新闻标题"></el-input>
    <div style="margin: 20px 0;"></div>
    <span>新闻概述:</span>
    <el-input v-model="news.summary" placeholder="概述"></el-input>
    <div style="margin: 20px 0;"></div>
    <span>新闻内容:</span>
    <el-input type="textarea" :autosize="{ minRows: 2, maxRows: 10}" placeholder="请输入新闻内容" v-model="news.content"></el-input>
    <div style="margin: 20px 0;"></div>
    <span>新闻图片:</span>
    <input type="file" ref="myPhoto" name="photo"/>
    <div style="margin: 20px 0;"></div>
    <span>新闻类型:</span>
    <el-select v-model="news.type" placeholder="请选择">
    <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value" ></el-option>
    </el-select>
    <div style="margin: 20px 0;"></div>
    <center><el-button align="center" type="primary" plain @click="saveEmp">发布新闻</el-button></center>
  </div>
</template>

<script>
export default {
name: "Login",
  data() {
    return {
      news:{},
      options: [{
          value: '财经',
          label: '财经'
        }, {
          value: '娱乐',
          label: '娱乐'
        }, {
          value: '国际',
          label: '国际'
        }, {
          value: '生活',
          label: '生活'
        }, {
          value: '政治',
          label: '政治'
        },{
          value: '体育',
          label: '体育'
        }],
    };
  },
  methods:{
    	saveEmp(){
				console.log(this.news); //新闻信息
				console.log(this.$refs.myPhoto.files[0]);//获取文件信息
        var userString=localStorage.getItem("user");
        this.user=JSON.parse(userString);
        this.news.writer=this.user.userName;
        this.news.userId=this.user.userId;
        this.news.content = this.preText(this.news.content);
				// 文件上传时 请求方式必须是post  enctype必须为multipart/form-data
				var formData = new FormData();
        formData.append("writer",this.news.writer);
        formData.append("userId",this.news.userId);
				formData.append("title",this.news.title);
				formData.append("summary",this.news.summary);
				formData.append("type",this.news.type);
				formData.append("content",this.news.content);
				formData.append("photo",this.$refs.myPhoto.files[0]);
				var _this = this;
				this.$axios({
					method:"post",
					url:"http://120.77.71.211:9090/mynewsweb/api/addNew",
					data:formData, 
					headers:{
						'content-type':'multipart/form-data'
					}
				}).then(res=>{
					console.log(res.data);
					if(res.data.state){
						if(window.confirm(res.data.msg)){
							location.href="#/mainTable";
						}else{
							_this.news = {};
						}
					}else{
						alert(res.data.msg);
					}
				});
			},
       preText (pretext) {
　　　　return pretext.replace('/\r\n/g', '<br>').replace('/\n/g', '<br>').replace('/\s/g', '&nbsp;')
      },
     returnMain(){
          this.$router.push("/mainTable");
      },
      
    },
    created(){
        var userString = localStorage.getItem("user");
        this.user = JSON.parse(userString);
            if (userString&&this.user.userKind=='创作者'||this.user.userKind=='管理员'||this.user.userKind=='申请成为创作者') {
                this.user = JSON.parse(userString);
            } else {
                alert("您没有权限");
                location.href = "#/mainTable";
            }
  }
  
};
</script>

<style>
    .add{
      width: 50%;
      margin:0 auto;
    }
</style>